#include "usermanager.h"

#include <memory>
#include <QJsonArray>

UserManager::~UserManager()
{

}

UserManager::UserManager() : m_token(""), m_chat_loaded(0), m_contact_loaded(0), m_user_info(nullptr)
{

}

void UserManager::setToken(QString token)
{
    m_token = token;
}

void UserManager::setUserInfo(std::shared_ptr<UserInfo> user_info)
{
    m_user_info = user_info;
}

int UserManager::getUid() const
{
    return m_user_info->m_uid;
}

QString UserManager::getName() const
{
    return m_user_info->m_name;
}

QString UserManager::getIcon()
{
    return m_user_info->m_icon;
}

std::shared_ptr<UserInfo> UserManager::getUserInfo()
{
    return m_user_info;
}

std::shared_ptr<FriendInfo> UserManager::getFriendById(int uid)
{
    auto find_it = m_friend_map.find(uid);
    if (find_it == m_friend_map.end()) {
        return nullptr;
    }

    return *find_it;
}

void UserManager::appendApplyList(QJsonArray array)
{
    // 遍历 QJsonArray 并输出每个元素
    for (const QJsonValue &value : array) {
        auto name = value["name"].toString();
        auto desc = value["desc"].toString();
        auto icon = value["icon"].toString();
        auto nick = value["nick"].toString();
        auto sex = value["sex"].toInt();
        auto uid = value["uid"].toInt();
        auto status = value["status"].toInt();
        auto info = std::make_shared<ApplyInfo>(uid, name, desc, icon, nick, sex, status);

        m_apply_list.push_back(info);
    }
}

void UserManager::appendFriendList(QJsonArray array)
{
    // 遍历 QJsonArray 并输出每个元素
    for (const QJsonValue& value : array) {

        auto name = value["name"].toString();
        auto desc = value["desc"].toString();
        auto icon = value["icon"].toString();
        auto nick = value["nick"].toString();
        auto sex = value["sex"].toInt();
        auto uid = value["uid"].toInt();
        auto back = value["back"].toString();

        // TODO
        auto info = std::make_shared<FriendInfo>(uid, name, nick, icon, sex, desc, back);
        m_friend_list.push_back(info);
        m_friend_map.insert(uid, info);
    }
}

void UserManager::appendFriendChatMsg(int friend_id, QVector<std::shared_ptr<TextChatData>> msgs)
{
    auto find_iter = m_friend_map.find(friend_id);
    if (find_iter == m_friend_map.end()) {
        qDebug() <<"append friend uid  " << friend_id << " not found";
        return;
    }

    find_iter.value()->appendChatMsgs(msgs);
}

bool UserManager::alreadyApply(int uid)
{
    for (const auto& apply : m_apply_list){
        if (apply->m_uid == uid){
            return true;
        }
    }

    return false;
}

void UserManager::addApplyList(std::shared_ptr<ApplyInfo> app)
{
    m_apply_list.push_back(app);
}

bool UserManager::isLoadChatFinish()
{
    if (m_chat_loaded >= m_friend_list.size()) {
        return true;
    }

    return false;
}

bool UserManager::IsLoadContactFinish()
{
    if (m_contact_loaded >= m_friend_list.size()) {
        return true;
    }

    return false;
}

void UserManager::updateChatLoadedCount()
{
    int begin = m_chat_loaded;
    int end = begin + CHAT_COUNT_PER_PAGE;

    if (begin >= m_friend_list.size()) {
        return ;
    }

    if (end > m_friend_list.size()) {
        m_chat_loaded = m_friend_list.size();
        return ;
    }

    m_chat_loaded = end;
}

void UserManager::updateContactLoadedCount()
{
    int begin = m_contact_loaded;
    int end = begin + CHAT_COUNT_PER_PAGE;

    if (begin >= m_friend_list.size()) {
        return;
    }

    if (end > m_friend_list.size()) {
        m_contact_loaded = m_friend_list.size();
        return;
    }

    m_contact_loaded = end;
}

// 检测添加的好友是否已近存在
bool UserManager::checkFriendById(int uid)
{
    auto iter = m_friend_map.find(uid);
    if (iter == m_friend_map.end()) {
        return false;
    }

    return true;
}

QVector<std::shared_ptr<ApplyInfo> > UserManager::getApplyList()
{
    return m_apply_list;
}


QVector<std::shared_ptr<FriendInfo> > UserManager::getConListPerPage()
{
    QVector<std::shared_ptr<FriendInfo>> friend_list;
    int begin = m_contact_loaded;
    int end = begin + CHAT_COUNT_PER_PAGE;

    if (begin >= m_friend_list.size()) {
        return friend_list;
    }

    if (end > m_friend_list.size()) {
        friend_list = QVector<std::shared_ptr<FriendInfo>>(m_friend_list.begin() + begin, m_friend_list.end());
        return friend_list;
    }

    friend_list = QVector<std::shared_ptr<FriendInfo>>(m_friend_list.begin() + begin, m_friend_list.begin() + end);

    return friend_list;
}

// 获取好友列表的其中一页(一页最多展示CHAT_COUNT_PER_PAGE)
QVector<std::shared_ptr<FriendInfo>> UserManager::getChatListPerPage()
{
    QVector<std::shared_ptr<FriendInfo>> friend_list;
    int begin = m_chat_loaded;
    int end = begin + CHAT_COUNT_PER_PAGE;

    if (begin >= m_friend_list.size()) {
        return friend_list;
    }

    // 更新结束的位置
    if (end > m_friend_list.size()) {
        friend_list = QVector<std::shared_ptr<FriendInfo>>(m_friend_list.begin() + begin, m_friend_list.end());
        return friend_list;
    }

    friend_list = QVector<std::shared_ptr<FriendInfo>>(m_friend_list.begin() + begin, m_friend_list.begin()+ end);

    return friend_list;
}

void UserManager::addFriend(std::shared_ptr<AuthRsp> auth_rsp)
{
    auto friend_info = std::make_shared<FriendInfo>(auth_rsp);
    m_friend_map[friend_info->m_uid] = friend_info;
}

void UserManager::addFriend(std::shared_ptr<AuthInfo> auth_info)
{
    auto friend_info = std::make_shared<FriendInfo>(auth_info);
    m_friend_map[friend_info->m_uid] = friend_info;
}



